﻿using SdmxMl.Structure;
using SdmxMl.Manager;
using System;

namespace SdmxMl.UC
{
    /// <summary>
    /// Control for ReportingTaxconomy Artefact attributes edition
    /// </summary>
    public partial class UcReportingTaxonomy : UcBaseAttrib
    {
        private ReportingTaxonomyType rpTax;

        #region C'tor

        public UcReportingTaxonomy()
        {
            InitializeComponent();
            customComboBoxName.DropDownControl = ucmlText;
            this.checkBoxIsFinal.CheckedChanged += new System.EventHandler(checkBoxIsFinal_CheckedChanged);
        }
        #endregion

        #region Binding

        private void IsFinalEditControls(bool isFinal)
        {
            bool enabled = isFinal == false;

            ucAgencyRef.IsFinal = isFinal;
            textBoxID.ReadOnly = isFinal;
            customComboBoxName.IsFinal = isFinal;
            comboMultiLinesDescr.IsFinal = isFinal;
            textBoxValidFrom.ReadOnly = isFinal;
            textBoxValidTo.ReadOnly = isFinal;
            textBoxVersion.ReadOnly = isFinal;
            textBoxUri.ReadOnly = isFinal;
            textBoxUrn.ReadOnly = isFinal;
            checkBoxIsExternalRef.Enabled = enabled;

            ucFlowRefListMeta.IsFinal = isFinal;
            ucFlowRefListData.IsFinal = isFinal;

        }

        protected override void BindToControls()
        {
            // Treats only ReportingTaxonomy
            rpTax = ar as ReportingTaxonomyType;
            if (rpTax == null)
                ar = null;

            if (ar != null)
            {
                inBinding = true;

                Visible = true;
                textBoxID.Text = ar.Id;
                textBoxVersion.Text = ar.Version;
                customComboBoxName.Text = ar.Name.ToString();
                ucmlText.ML = ar.Name;
                comboMultiLinesDescr.ML = ar.Description;
                ucAgencyRef.Text = ar.AgencyId;
                textBoxValidFrom.Text = ar.ValidFrom;
                textBoxValidTo.Text = ar.ValidTo;
                checkBoxIsFinal.Checked = ar.IsFinal;
                checkBoxIsExternalRef.Checked = ar.IsExternalReference;
                textBoxUri.Text = ar.Uri;
                textBoxUrn.Text = ar.Urn;

                ucFlowRefListMeta.Table = rpTax.MetaDataFlowRefList.AsDataTable();
                ucFlowRefListMeta.ArtefactType = SdmxArtefactType.Metadataflows;
                ucFlowRefListData.Table = rpTax.DataFlowRefList.AsDataTable(); ;
                ucFlowRefListData.ArtefactType = SdmxArtefactType.Dataflows;
                // Do we allow editing. 
                IsFinalEditControls(_isFinal);

                inBinding = false;

            }
            else
                Visible = false;
        }

        protected override void BindFromControls()
        {
            if (ar != null)
            {
                ar.Id = textBoxID.Text;
                ar.Version = textBoxVersion.Text;
                ar.Name.UpdateForCurrentLanguage(customComboBoxName.Text);


                ar.AgencyId = ucAgencyRef.Text;
                ar.ValidFrom = textBoxValidFrom.Text;
                ar.ValidTo = textBoxValidTo.Text;
                ar.IsFinal = checkBoxIsFinal.Checked;
                ar.IsExternalReference = checkBoxIsExternalRef.Checked;
                ar.Uri = textBoxUri.Text;
                ar.Urn = textBoxUrn.Text;

                rpTax.MetaDataFlowRefList.FromDataTable(ucFlowRefListMeta.Table);
                rpTax.DataFlowRefList.FromDataTable(ucFlowRefListData.Table);
            }
        }
        #endregion

        #region Resizing

        private void panelFlow_Resize(object sender, EventArgs e)
        {
            int middle = this.panelMiddle.Width;
            int width = (this.Width / 2) - middle / 2;
            this.panelLeft.Width = width;
            this.panelRight.Width = width;
        }
        #endregion

    }
}
